package jsyf.oa.service.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.system.AuditAndCopierDao;
import jsyf.oa.dao.system.CheckDao;
import jsyf.oa.dao.system.UserDao;
import jsyf.oa.entity.system.AuditAndCopierEntity;
import jsyf.oa.entity.system.UserEntity;
import jsyf.oa.facade.system.AuditAndCopierFacade;
import jsyf.oa.ientity.IBaseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class AuditAndCopierService implements AuditAndCopierFacade {

    @Autowired
    UserDao userDao;
    @Autowired
    AuditAndCopierDao auditAndCopierDao;
    @Autowired
    CheckDao checkDao;

    @Override
    public JSONObject getAuditAndCopierService(String jsonData) {
        JSONObject jsonObject = JSON.toJavaObject(JSON.parseObject(jsonData), JSONObject.class);
        String userId = jsonObject.getString("userId");
        String bigType = jsonObject.getString("bigType");
        String smallType = jsonObject.getString("smallType");
        String companyId = jsonObject.getString("companyId");
        List<JSONObject> auditList = new ArrayList<JSONObject>();
        List<JSONObject> copierList = new ArrayList<JSONObject>();
        List<String> userIds = new ArrayList<String>();
        Boolean flag = false;
        userIds.add(userId);
        JSONObject tan = null;
        List<IBaseEntity> auditEntityList = auditAndCopierDao.getIBaseEntityByCondition("AND BIG_TYPE=? AND SMALL_TYPE=? AND AUDIT_OR_COPIER_TYPE=0 ORDER BY SORT_NO", new Object[]{bigType, smallType});
        List<IBaseEntity> copierEntityList = auditAndCopierDao.getIBaseEntityByCondition("AND BIG_TYPE=? AND SMALL_TYPE=? AND AUDIT_OR_COPIER_TYPE=1 ORDER BY SORT_NO", new Object[]{bigType, smallType});
        if (bigType.equals("moneyApply")) {
            moneyApply((List<AuditAndCopierEntity>) (Object) auditEntityList, userId, auditList, companyId, userIds, true);
            Iterator iterator = auditList.iterator();
            while (iterator.hasNext()) {
                JSONObject str = (JSONObject) iterator.next();
                if ("2017121618514C22B68296F31AF8DDE2".equals(str.getString("id"))) {
                    tan = str;
                    iterator.remove();
                }
                if (companyId.equals("201712161613432EAF27DBCCB7D0E6CA") && str.getString("id").equals("2017121921544B71AAD69E89E3D5E5A7")) {
                    str.put("id", "20180515092241AFB1E3DB5CB25AA003");
                    str.put("name", "杨倩倩");
                    flag = true;
                }
            }
         /*
         //湖南公司最后加个贾总审核
            if (companyId.equals("2017121617484353B8D81EEAFB4598A6")) {
                JSONObject JXDJson = new JSONObject();
                JXDJson.put("auditFlag", "-1");
                JXDJson.put("id", "201712161851433BAD8487BE30159C9E");
                JXDJson.put("name", "贾贤达");
                JXDJson.put("roleName", userDao.getViewByUserId(" AND ID=? ", new Object[]{"201712161851433BAD8487BE30159C9E"}).get(0).getString("roleName"));
                auditList.add(JXDJson);
            }*/
            if (null != tan) {
                auditList.add(tan);
            }
        }
        moneyApply((List<AuditAndCopierEntity>) (Object) copierEntityList, userId, copierList, companyId, userIds, false);
        if (flag) {
            JSONObject str = new JSONObject();
            str.put("id", "2017121921544B71AAD69E89E3D5E5A7");
            str.put("name", "张芳");
            copierList.add(str);
        }
        JSONObject result = new JSONObject();
        result.put("audit", auditList);
        result.put("copier", copierList);
        return result;
    }

    @Override
    public Boolean save(String jsonData) {
        JSONObject jsonObject = JSON.toJavaObject(JSON.parseObject(jsonData), JSONObject.class);
        List<JSONObject> jsonParams = FastJsonConvert.convertJSONToArray(jsonObject.getString("list"), JSONObject.class);
        for (int i = 0; i < jsonParams.size(); i++) {
            AuditAndCopierEntity auditAndCopierEntity = JSON.toJavaObject(jsonParams.get(i), AuditAndCopierEntity.class);
            auditAndCopierDao.add(auditAndCopierEntity);
        }
        return true;
    }

    private JSONObject getUser(String userId, List<String> userIds) {
        List<JSONObject> users = userDao.getViewByUserId("AND ID=? AND jobState!=1", new Object[]{userId});
        JSONObject user = null;
        if (null != users && users.size() > 0) {
            JSONObject oldUser = users.get(0);
            Iterator<String> listIt = userIds.iterator();
            while (listIt.hasNext()) {
                if (listIt.next().equals(oldUser.getString("id"))) {
                    return null;
                }
            }
            user = new JSONObject();
            user.put("id", oldUser.getString("id"));
            user.put("name", oldUser.getString("name"));
            user.put("roleName", oldUser.getString("roleName"));
            user.put("auditFlag", "-1");
        }
        return user;
    }

    private void moneyApply(List<AuditAndCopierEntity> auditOrCopierEntityList, String userId, List<JSONObject> resultList, String companyId, List<String> userIds, Boolean flag) {
        if (null != auditOrCopierEntityList && auditOrCopierEntityList.size() > 0) {
            JSONObject user = null;
            for (int i = 0; i < auditOrCopierEntityList.size(); i++) {
                AuditAndCopierEntity auditOrCopierEntity = auditOrCopierEntityList.get(i);
                switch (auditOrCopierEntity.getPersonType()) {
                    case "直接上级":
                        user = getUser(((UserEntity) userDao.getIBaseEntityById(userId)).getParentId(), userIds);
                        if (null != user) {
                            userIds.add(user.getString("id"));
                            resultList.add(user);
                        }
                        break;
                    case "岗位":
                        String parentIds = checkDao.getParentByUserId(userId, true);
                        List<JSONObject> users = userDao.getUserByCompanyIdAndRoleName(userId, auditOrCopierEntity.getAuditOrCopier(), companyId, parentIds);
                        if (null != users && users.size() > 0) {
                            //flag == true 只查一个人  false 查所有(抄送人用)
                            if (flag) {
                                user = getUser(users.get(0).getString("userId"), userIds);
                                if (null != user) {
                                    userIds.add(user.getString("id"));
                                    resultList.add(user);
                                }
                            } else {
                                for (int j = 0; j < users.size(); j++) {
                                    user = getUser(users.get(j).getString("userId"), userIds);
                                    if (null != user) {
                                        userIds.add(user.getString("id"));
                                        resultList.add(user);
                                    }
                                }
                            }
                        }
                        break;
                    case "人":
                        for (IdAndName idAndName : IdAndName.values()) {
                            if (auditOrCopierEntity.getAuditOrCopier().equals(idAndName.getUserName())) {
                                user = getUser(idAndName.getUserId(), userIds);
                                if (null != user) {
                                    userIds.add(user.getString("id"));
                                    resultList.add(user);
                                }
                            }
                        }
                        break;
                }
            }
        }
    }

}

enum IdAndName {

    CAOGUOYU("201711271733404C9DA1461F2421F828", "曹国玉"),
    DENGJIYING("201712171253420DBBFAE64C57F955B9", "邓继英"),
    JIAXIANDA("201712161851433BAD8487BE30159C9E", "贾贤达"),
    JINGYUESHUANG("201712171257484DA20FEC7F96A76B3D", "靳悦双"),
    HOUJIAOYAN("20171217130247059F2B10DB2BE1B5C7", "侯娇艳"),
    TANZONG("2017121618514C22B68296F31AF8DDE2", "谈小中"),
    LINENZHI("20171219122040B28198B030F64B7D37", "林恩智"),
    SUNQIANG("20180125095744209CA7B57B18576F6C", "孙强"),
    ZHANGFANG("2017121921544B71AAD69E89E3D5E5A7", "张芳"),
    MAJIANGFAN("2018012211114F47998BBAD3CEF1284A", "马江帆"),
    SUIHUA("20180307111342A6AFC442DC05FD86E2", "隋华"),
    YUANYIMING("2018072410074E31BDA9D2DDCC10562B", "袁一鸣"),
    HEJIANRONG("20181029131941189298E733B95017AF", "贺建荣");

    private String userId;
    private String userName;

    private IdAndName(String userId, String userName) {
        this.userId = userId;
        this.userName = userName;
    }

    public String getUserId() {
        return userId;
    }

    public String getUserName() {
        return userName;
    }
}


